package com.baosight.sczx.zm.cs.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.FrameworkInfo;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.zm.cs.domain.ZMCSBusinessFilterDetails;

/**隶属于系统设置模块：部门列表
 * <p>Title: ServiceZMCSBusinessFilterDetails.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张志芳
 * @version 1.0
 * <pre>Histroy:
 *          2011-1-5 下午03:55:50  张志芳  Create
 *</pre>
 */
public class ServiceZMCS18 extends ServiceEPBase {
	
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS18</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-16上午09:09:52 严洪浩
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		
		outInfo.addBlock(Department.getInstance().getConstructionUnit(false));//单位部门
		String runType = FrameworkInfo.getProjectType();
		outInfo.set("runType", runType);//运行状态
		
		return outInfo;
	}
	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS18</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:56:44 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdBusinessFilterId = inInfo.getString("fdBusinessFilterId");
		StaticTools.setQueryParameters(inInfo, "fdBusinessFilterId", fdBusinessFilterId, true);//添加查询条件
		EiInfo outInfo = super.query(inInfo, "ZMCSBusinessFilterDetails.query", new ZMCSBusinessFilterDetails());

		return outInfo;
	}
	
	/**
	 * 新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS18</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:56:59 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		//验证重复
		boolean validate = validate(inInfo, "ZMCSBusinessFilterDetails.validate", new HashMap<String, String>());
		
		if(validate) {
			return inInfo;
		}
		else {
			String fdBusinessFilterId = inInfo.getString("fdBusinessFilterId");
			inInfo.getBlock("result").setCell(0, "fdBusinessFilterId", fdBusinessFilterId);//主表ID
			inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CI_BUSINESSFILTERDETAILS"));//主键

			EiInfo outinfo = super.insert(inInfo, "ZMCSBusinessFilterDetails.insert", "result");
			return outinfo;
		}
	}

	/**
	 * 修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS18</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:57:14 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo outInfo = super.update(inInfo, "ZMCSBusinessFilterDetails.update");
		return outInfo;
	}
	
	/**
	 * 删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS18</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-6下午01:57:29 张志芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo _temp = super.delete(inInfo, "ZMCSBusinessFilterDetails.delete");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: validate</p>
	 * <p>Description: 校验编号和名称的重复性</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-18下午03:53:05 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdDepartmentId = inInfo.getBlock("result").getCellStr(0, "fdDepartmentId");
		String fdBusinessFilterId = inInfo.getBlock("result").getCellStr(0, "fdBusinessFilterId");
		params.put("fdDepartmentId", fdDepartmentId);
		params.put("fdBusinessFilterId", fdBusinessFilterId);
		
		List result = dao.query(sqlName, params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("该部门已经添加,不能重复添加！");
			return true;
		}
		return false;
	}
}
